package com.kuaiditu.assistant.module.retrofit.impl;

import com.alibaba.fastjson.JSON;
import com.kuaiditu.assistant.api.xz_api.YTBaseRequest;
import com.kuaiditu.assistant.api.xz_api.YTBaseResponse;
import com.kuaiditu.assistant.module.retrofit.ConverterAdapter;

import java.io.IOException;

/**
 * Created by Eenie on 2017/9/7.
 * Email: eeniework@gmail.com
 * Des:
 */

public class YTConverterAdapter implements ConverterAdapter<YTBaseRequest, YTBaseResponse> {

    @Override
    public String onConverterRequest(YTBaseRequest request) throws IOException {
        return request.toString();
    }

    @Override
    public String onConverterResponse(YTBaseResponse response) throws IOException {
        if (response.isSuccess()) {
            return JSON.toJSONString(response);
        } else {
            throw new IOException(response.getPrompt());
        }
    }

    @Override
    public YTBaseResponse strToObj(String str) {
        return JSON.parseObject(str, YTBaseResponse.class);
    }
}
